#include "IntListSet.h"

// ( 2 ^ 32  ) / 2 
#define INT_MIN_VALUE 0x80000000

IntListSet::IntListSet(){
	pnext = NULL;
	inext = NULL;
	value = INT_MIN_VALUE; //Default Value -> int.minvalue
}

IntListSet::~IntListSet(){
	if( pnext != NULL ){
		delete pnext; 
	}
}

bool IntListSet::contains( int v ){

	if( value == v ) return true;

	if( pnext != NULL ){
		return pnext->contains(v);
	}

	return false;
}

void IntListSet::add( int v ){	
	if( value == INT_MIN_VALUE ){ //Recursive implementation
		value = v;
	}else{
		if( pnext == NULL ){
			pnext = new IntListSet();			
		}
		pnext->add(v);
	}
}

void IntListSet::rem( int v ){	
	if( value == v ){
		value = INT_MIN_VALUE; //Default Value -> int.minvalue
		return;
	}

	if( pnext != NULL ){
		pnext->rem(v);
	}
}

IntIterator* IntListSet::newIterator(){
	inext = this;
	return (IntIterator*)this;
}

bool IntListSet::hasNext(){
	return inext != NULL;
}

int IntListSet::next(){
	int rv = inext->value;
	do{
		inext = inext->pnext; 
	}while( (inext != NULL) && (inext->value == INT_MIN_VALUE) );

	return rv;
}